package org.niit.app.online

import org.apache.spark.sql.SparkSession
import org.niit.service.RecommendService

/**
 * 推荐系统应用入口
 */
object RecommendApp {
  def main(args: Array[String]): Unit = {
    // 创建SparkSession
    val spark = SparkSession
      .builder()
      .appName("TakeawayRecommendation")
      .master("local[*]")
      .config("spark.sql.warehouse.dir", "spark-warehouse")
      .config("spark.driver.memory", "4g")
      .config("spark.executor.memory", "4g")
      .config("spark.memory.offHeap.enabled", "true")
      .config("spark.memory.offHeap.size", "2g")
      .config("spark.storage.level", "MEMORY_AND_DISK")
      .config("spark.shuffle.manager", "sort")
      .config("spark.shuffle.spill.compress", "true")
      .getOrCreate()
    
    // 设置日志级别
    spark.sparkContext.setLogLevel("WARN")
    
    try {
      println("===================== 外卖订单推荐系统应用 ======================")
      
      // 数据文件路径
      val dataPath = "F:/code/Takeaway-master/input/takeaway.txt"
      
      // 执行所有推荐算法
      RecommendService.runAllRecommendations(spark, dataPath)
      
      println("===================== 推荐系统运行完成 =====================")
    } catch {
      case e: Exception =>
        println("推荐系统过程中发生错误:")
        e.printStackTrace()
    } finally {
      // 停止SparkSession
      spark.stop()
    }
  }
} 